package com.aiit.service.impl;

import com.aiit.constant.StatusConstant;
import com.aiit.context.BaseContext;
import com.aiit.dto.EmployeePageQueryDTO;
import com.aiit.dto.TeaDTO;
import com.aiit.dto.TeaOrderDTO;
import com.aiit.entity.Tea;
import com.aiit.entity.TeaOrder;
import com.aiit.result.PageResult;
import com.aiit.service.TeaService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.annotation.WebServlet;
import java.time.LocalDateTime;
import java.util.List;

@Service
@WebServlet("/verifyCode")
public class TeaServiceImpl implements TeaService {

    @Autowired
    private com.aiit.mapper.TeaMapper teaMapper;

    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());

        Page<Tea> teas = teaMapper.pageQuery(employeePageQueryDTO);

        long total = teas.getTotal();
        List<Tea> records = teas.getResult();

        return new PageResult(total, records);
    }


    public void update(TeaDTO teaDTO) {
        Tea tea = new Tea();
        BeanUtils.copyProperties(teaDTO, tea);

        tea.setUpdateTime(LocalDateTime.now());
        tea.setUpdateUser(BaseContext.getCurrentId());

        teaMapper.update(tea);
    }


    public void save(TeaDTO teaDTO) {
        System.out.println("当前线程的id:" + Thread.currentThread().getId());
        Tea tea = new Tea();

        //对象属性拷贝
        BeanUtils.copyProperties(teaDTO, tea);

        tea.setStatus(StatusConstant.ENABLE);

        tea.setCreateTime(LocalDateTime.now());
        tea.setUpdateTime(LocalDateTime.now());

        //设置当前记录创建人id和修改人的id
        // TODO 后期需要改为当前登录用户的id
        tea.setCreateUser(BaseContext.getCurrentId());
        tea.setUpdateUser(BaseContext.getCurrentId());

        teaMapper.insert(tea);
    }

    @Override
    public void deleteTea(String name) {
        teaMapper.deleteById(name);
    }

    @Override
    public List<Tea> list(Tea tea) {
        List<Tea> list = teaMapper.list(tea);
        return list;
    }

    @Override
    public void saveOrder(TeaOrderDTO teaOrderDTO) {
        System.out.println("当前线程的id:" + Thread.currentThread().getId());
        TeaOrder teaOrder = new TeaOrder();

        //对象属性拷贝
        BeanUtils.copyProperties(teaOrderDTO, teaOrder);

        teaOrder.setStatus(StatusConstant.ENABLE);

        teaOrder.setCreateTime(LocalDateTime.now());
        teaOrder.setUpdateTime(LocalDateTime.now());

        //设置当前记录创建人id和修改人的id
        // TODO 后期需要改为当前登录用户的id
        teaOrder.setCreateUser(BaseContext.getCurrentId());
        teaOrder.setUpdateUser(BaseContext.getCurrentId());

        teaMapper.insertOrder(teaOrder);
        teaMapper.updateRemaining(teaOrder);
        teaMapper.updateRemaining2(teaOrder);
    }
}



